"use strict";
/* Controllers */
app.controller("dataMapListController", ['$scope', '$location', '$rootScope', '$routeParams', 'dataMapListService', function ($scope, $location, $rootScope, $routeParams, dataMapListService) {

    var vm = $scope.vm = {};
    vm.p = {};
    vm.result = {};
    vm.p.appID = $routeParams.appID; //  获取应用模块的名称

    var errorFn = function (result) {
        alert(result);
    };

    // 数据地图仅仅到数据域（主题域）最细到主题和表
    vm.conf = [
        //{text:"HRG-B/C侧", nodes:[
        //    {text:"流量域", nodes:["招聘C流量主题"]},
        //    {key:"服务域",value:["商业产品转化主题","简历连接转化主题","魔方业务效率主题"]},
        //    {key:"客户域",value:["招聘B流量主题","商业流量"]},
        //    {key:"用户域",value:["用户主题","简历主题"]},
        //    {key:"连接域",value:["投递主题","微聊主题","电话主题","直播主题","神奇面试间主题"]},
        //    {key:"营销域",value:["用户投放主题"]},
        //    {key:"交易域",value:["订单主题"]},
        //    {key:"风控域", value:["风控主题"]}
        //]},
        //{key:"商业联盟侧", value:[
        //    {key:"营销域",value:["商业流量转化","用户推广数据(投放)"]},
        //    {key:"流量域",value :["全站ROI"]},
        //    {key:"营销域",value:["DSP主题","合作媒体主题","信息流(FEED)主题","SEM主题"]}
        //]},
        //{key:"ADS经分侧", value:[
        //    {key:"交易域",value:["收入主题"]},
        //    {key:"客户域",value :["客户主题"]},
        //    {key:"流量域",value:["流量主题"]},
        //    {key:"连接域",value:["连接主题"]},
        //    {key:"营销域",value:["商机增长","投放主题"]}
        //]},
        //{key:"其他",value:[
        //    {key:"CMC",value:["CMC类别","CMC地区","CMC时间"]},
        //    {key:"支撑",value:["定价折扣","IMC","IP服务"]},
        //    {key:"产品",value:["关键词售卖","Product产品"]}
        //]}
    ];

    $scope.queryItems = function(id) {
        $scope.listMetricSource(id);
    };

    $scope.searchItems = function() {
        var vm = $scope.vm;
        vm.result.ms = "...";
        // 去除两端空格
        vm.p.search = vm.p.search.replace(/^\s+|\s+$/g,'');
        if(vm.p.search=='') {
            alert("请输入搜索表名称");
            return;
        }
        var params = {search: vm.p.search};
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };

        dataMapListService.searchMetricSource(params, procFn, errorFn);
    }

    $scope.listMetricSource = function (topic) {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {topic: topic};
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        dataMapListService.listMetricSource(params, procFn, errorFn);
    };

    $scope.listMetricTopic = function () {
        var vm = $scope.vm;
        vm.result.ms = "...";
        var params = {};
        var procFn = function (response) {
            if ("000" !== response.code) {
                alert(response.message);
                return ;
            }
            vm.result.ms = response.ms;
            var data = response.data;
            if (data == null) return;

            vm.conf = list2Tree(data);

        };
        dataMapListService.listMetricTopic(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        vm.result.ms = '';
        $scope.listMetricTopic();

        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });

//        $resultTable.bootstrapTable('hideColumn', 'depName');
        $resultTable.bootstrapTable('hideColumn', 'sourceRelation');

//        $scope.listMetricSource('招聘C流量主题');
        $scope.listMetricSource('all');


    };

    $scope.initializeController();

}]);


/* Services */
app.service('dataMapListService', ['ajaxService', function (ajaxService) {

    this.listMetricTopic = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_map/listMetricTopic.do", successFunction, errorFunction);
    };
    this.listMetricSource = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_map/listMetricSource.do", successFunction, errorFunction);
    };
    this.searchMetricSource = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/data_map/searchMetricSource.do", successFunction, errorFunction);
    };

}]);



function metricSourceIDFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        '<a href="https://dp.58corp.com/data-map/detail-page/', row.topicTblId,'" target="_blank">', "<u>DP详情</u>" , '</a> ',
        '<a href="#/star/metric/data_map_add/id/', row.id,'" target="_blank">', "<u>修改</u>" , '</a>'
    ].join('');
}


function topicTblFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    return [
        //value + '<a href="https://dp.58corp.com/data-map/detail-page/', row.topicTblId,'" target="_blank">', " <u>查看</u>" , '</a>'
        '<a href="#/star/metric/data_map_item/code/', row.topicTbl,'" target="_blank">', " <u>" +value+"</u>" , '</a>'
    ].join('');
}


function sourceRelationFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var result = [];
    var items = value.split(',');
    for(var i=0;i<items.length; ++i) {
        //var item = [items[i] + '<a href="https://dp.58corp.com/data-map/detail-page/', items[i],'" target="_blank">', " <u>查看</u>" , '</a>'].join('');
        var item = items[i];
        result.push(item);
    }
    return result.join('<br/>');
}

// 格式为： {表描述},{表备注}
// 表描述： 即表是什么 ,表备注：即表的计算逻辑以及需要注意项
function topicTblDescFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var items = value.split(/,|，|。/);
    value = items[0];

    var result = [];
    var MAX_LEN= 30;
    for(var i=0;i<value.length;) {
        var item = value.substr(i, MAX_LEN);
        result.push(item);
        i += MAX_LEN;
    }
    return result.join('<br/>');
}

function businessOwnerIdFmt(value, row, index) {
    if (value == null || value=='0' || value=='') {
        return row.ownerId;
    }
    return value;
}



